package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.RetestAdjustmentMapper;
import com.bjfu.gmms.model.RetestAdjustment;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/adjustmentSubmit")
public class AdjustmentSubmitServlet extends HttpServlet {

    private RetestAdjustmentMapper retestAdjustmentMapper;



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 使用 try-with-resources 来确保 SqlSession 被自动关闭
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession("development")) {
            retestAdjustmentMapper = sqlSession.getMapper(RetestAdjustmentMapper.class);
            // 获取请求参数
            int studentId = Integer.parseInt(request.getParameter("studentId"));
            boolean isAdjustmentAccepted = Boolean.parseBoolean(request.getParameter("isAdjustmentAccepted"));

            RetestAdjustment retestAdjustment=new RetestAdjustment();
            retestAdjustment.setStudentId(studentId);
            retestAdjustment.setAdjustmentAccepted(isAdjustmentAccepted);
            // 调用存储过程更新调剂信息
            retestAdjustmentMapper.updateStudentRetestAdjustment(retestAdjustment);

            // 提交事务
            sqlSession.commit();

            // 重定向到成功页面
            /*response.sendRedirect("businessTwo.jsp");*/
            response.sendRedirect("studentDashboard.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException("Failed to update adjustment", e);
        }
    }
}

